package com.controller.advice;

import com.domain.HttpResponse;
import com.domain.ex.Codes;
import com.domain.page.Paginator;
import com.dto.AdviceDto;
import com.dto.NoteDto;
import com.dto.UserExtDto;
import com.google.common.collect.Maps;
import com.mapper.generator.ShAdviceMapper;
import com.pojo.ShAdvice;
import com.pojo.ShCompany;
import com.service.advice.AdviceService;
import com.service.user.UserService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.security.provider.SHA;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2015/11/22 0022.
 */
@Controller
@RequestMapping("advice")
public class AdviceController {
  private static final Logger LOG = LoggerFactory.getLogger(AdviceController.class);

  @Autowired
  private AdviceService adviceService;
  @Autowired
  private UserService userService;

  // - 发表留言
  @RequestMapping(value = "add")
  @ResponseBody
  public Object add(@Param("content") String content, @Param("fee") BigDecimal fee, @Param("msgType") Integer msgType, @Param("isShowMobile") Integer isShowMobile, @Param("endTime") String endTime,@Param("img")String img,@Param("address")String address, HttpServletRequest request) {
    NoteDto note = adviceService.add(request.getSession().getAttribute("email").toString(), content, msgType, fee, isShowMobile, endTime, img, address);

    return HttpResponse.create(Codes.OK, "", note);
  }


  // - 消息列表
  @RequestMapping("list")
  @ResponseBody
  public Object list(@Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize,@Param("type")Integer type,@Param("status")Integer status,@Param("email")String email) {

    return adviceService.list(pageNo, pageSize,type,status,email);
  }

  // - 跳转到吐槽页面
  @RequestMapping("advice")
  @ResponseBody
  public ModelAndView advice(@Param("type")Integer type,HttpServletRequest request) {

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("type",type);
    return modelAndView;
  }

  // - 领赏
  @RequestMapping("reward")
  @ResponseBody
  public Object reward(HttpServletRequest request, @Param("uuid") String uuid, @Param("email") String email) {
    if (request.getSession().getAttribute("email") == null) {
      return HttpResponse.create(Codes.SystemDeclare, "请先登录");
    }
    String dealEmail = request.getSession().getAttribute("email").toString();
    return HttpResponse.create(Codes.OK, "", adviceService.reward(uuid, dealEmail, email));
  }

  // - 跳转到竞价页面
  @RequestMapping("toAuction")
  @ResponseBody
  public ModelAndView toAuction(@Param("uuid") String uuid) {

    ModelAndView modelAndView = new ModelAndView();
    AdviceDto adviceDto = adviceService.queryByUuid(uuid);
    adviceDto.setEndTime(adviceDto.getEndTime().substring(0, adviceDto.getEndTime().length() - 2));
    modelAndView.addObject("advice", adviceDto);
    modelAndView.setViewName("/advice/auction");
    return modelAndView;
  }

}
